package com.fuhao.orderservice.service.Impl;

import com.fuhao.orderservice.model.Order;
import com.fuhao.orderservice.service.OrderClient;
import com.fuhao.orderservice.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderClient orderClient;
    Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public Order save(Integer userId, Integer productId) {
        logger.info("service save");
        //直接在实现类中调用该接口，就会远程调用需要的服务
        Map<String,Object> map = orderClient.findById(productId);
        //JsonNode jsonNode = JsonUtil.str2JsonNode(str);
        Order order = new Order();
        order.setId(UUID.randomUUID().toString());
        order.setCreatetime(new Date());
        order.setTradeNum(UUID.randomUUID().toString());
        order.setUserId(userId);
        order.setPrice(Integer.parseInt(map.get("price").toString()));
        order.setProductName(map.get("name").toString());
        return order;
    }

//    @Autowired
//    private RestTemplate restTemplate;

//    @Override
//    public Order save(Integer userId, Integer productId) {
//        Map<String,Object> map = restTemplate.getForObject("http://product-service/api/v1/product/findById?id="+productId, Map.class);
//        System.out.println(map);
//        Order order = new Order();
//        order.setId(UUID.randomUUID().toString());
//        order.setCreatetime(new Date());
//        order.setTradeNum(UUID.randomUUID().toString());
//        order.setUserId(userId);
//        order.setPrice(Integer.parseInt(map.get("price").toString()));
//        order.setProductName(map.get("name").toString());
//        return order;
//    }


}
